package ai.zixing.mashibing.basic_class.class07;

import sun.nio.cs.ext.MacHebrew;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

// 统计宽度最大的层级
public class Code06_TreeMaxWidth {

    public static int maxWidthUseMap(Node head) {
        if (head == null) {
            return 0;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        // 统计key 在哪一层
        HashMap<Node, Integer> levelMap = new HashMap<>();
        levelMap.put(head, 1);
        // 当前层【在统计哪一层的宽度】
        int curLevel = 1;
        // 当前 curLevel 层，目前宽度是多少【出来的时候再++】
        int curLevelNodes = 0;
        int max = 0;
        Node cur = null;
        while (!queue.isEmpty()) {
            cur = queue.poll();
            int curNodeLevel = levelMap.get(cur);
            if (cur.left != null) {
                levelMap.put(cur.left, curNodeLevel + 1);
                queue.add(cur.left);
            }
            if (cur.right != null) {
                levelMap.put(cur.right, curNodeLevel + 1);
                queue.add(cur.right);
            }
            // 当前统计的节点层就是当前层
            if (curNodeLevel == curLevel) {
                curLevelNodes++;
            }
            // 结算上一层节点
            else {
                max = Math.max(max, curLevelNodes);
                curLevel++;
                curLevelNodes = 1;
            }
        }
        // 全部弹出之后还需要对比最后一层的节点数
        return Math.max(max, curLevelNodes);
    }
    public static int maxWidthNoMap(Node head) {
        if (head == null) {
            return 0;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        // 当前层的最右一个节点
        Node curEnd = head;
        // 如果有下一层，最右节点
        Node nextEnd = null;
        int max = 0;
        // 当前层的节点数
        int curLevelNodes = 0;
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            // 当前层更新下一层的最右节点
            if (cur.left != null) {
                queue.add(cur.left);
                nextEnd = cur.left;
            }
            if (cur.right != null) {
                queue.add(cur.right);
                nextEnd = cur.right;
            }
            curLevelNodes++;
            // 每一层结束后直接结算
            if (cur == curEnd) {
                max = Math.max(max, curLevelNodes);
                curLevelNodes = 0;
                curEnd = nextEnd;
            }
        }
        return max;
    }


    public static Node generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    public static Node generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxLevel));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    public static void main(String[] args) {
        int maxLevel = 10;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++) {
            Node head = generateRandomBST(maxLevel, maxValue);
            if (maxWidthNoMap(head) != maxWidthUseMap(head)) {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish");
    }
}
